package com.zw.stock_sell_storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zw.stock_sell_storage.dto.CustomerDto;
import com.zw.stock_sell_storage.dto.MyPageDto;
import com.zw.stock_sell_storage.dto.SupplierDto;
import com.zw.stock_sell_storage.mapper.CustomerMapper;
import com.zw.stock_sell_storage.service.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, CustomerDto> implements ICustomerService {
    @Autowired
    CustomerMapper customerMapper;

    @Override
    public Page<CustomerDto> queryCustomerList(MyPageDto<CustomerDto> myPageDto) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (myPageDto.getData().getCustomerName()!=null&&!"".equals(myPageDto.getData().getCustomerName())){
            queryWrapper.like("customer_name",myPageDto.getData().getCustomerName());
        }
        if (myPageDto.getData().getCustomerBusinessScope()!=null&&!"".equals(myPageDto.getData().getCustomerBusinessScope())){
            queryWrapper.like("customer_business_scope",myPageDto.getData().getCustomerBusinessScope());
        }
        return customerMapper.selectPage(new Page<>(myPageDto.getCurrentPage(),myPageDto.getPageSize()),queryWrapper);
    }

    @Override
    public CustomerDto queryCustomerOne(int customerId) {
        return customerMapper.selectById(customerId);
    }

    @Override
    @Transactional
    public boolean updateCustomer(CustomerDto customerDto) {

        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("customer_id",customerDto.getCustomerId());
        customerMapper.update(customerDto,updateWrapper);
        return true;
    }

    @Override
    @Transactional
    public boolean addCustomer(CustomerDto customerDto) {
        customerMapper.insert(customerDto);
        return true;
    }

    @Override
    public List<CustomerDto> queryAllCustomerList() {
        return customerMapper.selectList(null);
    }
}
